package com.sina.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import weibo4j.Oauth;
import weibo4j.Timeline;
import weibo4j.Weibo;
import weibo4j.http.AccessToken;
import weibo4j.model.Status;
import weibo4j.model.StatusWapper;
import weibo4j.model.WeiboException;
import weibo4j.util.BareBonesBrowserLaunch;

public class Main {
	
	public static void main(String[] args) throws WeiboException, IOException{
        Oauth oauth = new Oauth();
        BareBonesBrowserLaunch.openURL(oauth.authorize("code"));
        System.out.print("Hit enter when it's done.[Enter]:");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String code = br.readLine();
       
        System.out.println("code: " + code);
        try{
        		AccessToken accessToken = oauth.getAccessTokenByCode(code);
                System.out.println(accessToken);
                
                Weibo weibo = new Weibo();
                weibo.setToken(accessToken.getAccessToken());
                
                Timeline timeLine = new Timeline();
                StatusWapper s = timeLine.getPublicTimeline();
                List<Status> status = s.getStatuses();
                for(Status st : status){
                	 System.out.println(st.getText());
                }
               
        } catch (WeiboException e) {
                if(401 == e.getStatusCode()){
               
                        System.out.println("Unable to get the access token.");
                        
                }else{
                        e.printStackTrace();
                }
        }
}
}
